{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e18c8e8",
   "metadata": {},
   "source": [
    "----\n",
    "* 2023-11-20\n",
    "* 课程：python\n",
    "* week10\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b990b101",
   "metadata": {},
   "source": [
    "# 1.字典的创建\n",
    "\n",
    "> 1. 按照数据结构形态创建：[],(),{}\n",
    "> 2. 按照数据结构的方法创建：list(),tuple(),set(),dict()\n",
    "> 3. 根据自身的性质创建：\n",
    ">> 1. [],append\n",
    ">> 2. value1,value2,value3\n",
    ">> 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0183fa0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jessa', 'country': 'usa', 'telephone': 1178}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 方法1：\n",
    "person = {\n",
    "    \"name\":\"jessa\",\n",
    "    \"country\":\"usa\",\n",
    "    \"telephone\":1178\n",
    "}\n",
    "print(person)\n",
    "print(type(person))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af0d02b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jessa', 'country': 'usa', 'telephone': 1178}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 方法2：\n",
    "person = dict(\n",
    "    name = \"jessa\",\n",
    "    country = \"usa\",\n",
    "    telephone = 1178\n",
    ")\n",
    "print(person)\n",
    "print(type(person))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "178f5d46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jessa', 'country': 'usa', 'telephone': '1178'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 方法3：字典的新增\n",
    "person = {}\n",
    "person['name'] = \"jessa\"\n",
    "person['country'] = \"usa\"\n",
    "person['telephone'] = \"1178\"\n",
    "print(person)\n",
    "print(type(person))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6de01605",
   "metadata": {},
   "source": [
    "# 2.字典的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2373b248",
   "metadata": {},
   "source": [
    "## 2.1 查询\n",
    "> 1. in方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7db946e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'name' in person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6a262d6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'jessa' in person #查字典查询的是关键字【key】前面的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e7bec7f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jessa'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.get('name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4d15a2e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "person.get('jessa')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "10e1b778",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jessa\n"
     ]
    }
   ],
   "source": [
    "keys = 'name'\n",
    "if 'name' in person:\n",
    "    print(person.get(keys))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdddee2e",
   "metadata": {},
   "source": [
    "> 2.字典的items方法（关于字典的循环）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9612eda6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('name', 'jessa'), ('country', 'usa'), ('telephone', '1178')])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "103ec773",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "country\n",
      "telephone\n"
     ]
    }
   ],
   "source": [
    "# 循环一个元素得到结果是key\n",
    "for i in person:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5a622124",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name jessa\n",
      "country usa\n",
      "telephone 1178\n"
     ]
    }
   ],
   "source": [
    "for k,v in person.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3b58bb04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明': 80, '小红': 85, 'jessa': 95}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例:需求【找出所有成绩大于九十分的同学】\n",
    "python_成绩 = {\n",
    "    \"小明\":80,\n",
    "    \"小红\":85,\n",
    "    \"jessa\":95\n",
    "}\n",
    "python_成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7c609a53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jessa 95\n"
     ]
    }
   ],
   "source": [
    "for k,v in python_成绩.items():\n",
    "    if v > 90:\n",
    "        print(k,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76ba2fef",
   "metadata": {},
   "source": [
    "> 3. 字典的key，values方法\n",
    ">> 1. keys:可以查看所有关键词，大概了解一下有哪些属性\n",
    ">> 2. values:可以查看所有的结果，大致了解一下结果的特征。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d66f8425",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'country', 'telephone'])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8e3f3c72",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "96201051",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate, br',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.31.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-655b4b97-0e66c330107a91493d2cb4f1'},\n",
       " 'origin': '120.85.142.52',\n",
       " 'url': 'https://httpbin.org/get'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.get(\"https://httpbin.org/get\")\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "232724fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['args', 'headers', 'origin', 'url'])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "408c461c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Accept', 'Accept-Encoding', 'Host', 'User-Agent', 'X-Amzn-Trace-Id'])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results['headers'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2cf8ab51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['jessa', 'usa', '1178'])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc75afdb",
   "metadata": {},
   "source": [
    "> 4.字典的删除：pop"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97a7557a",
   "metadata": {},
   "source": [
    "---------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "45e748e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jessa', 'country': 'usa', 'telephone': '1178'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "person = {}\n",
    "person['name'] = \"jessa\"\n",
    "person['country'] = \"usa\"\n",
    "person['telephone'] = \"1178\"\n",
    "print(person)\n",
    "print(type(person))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "989dd801",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jessa', 'country': 'usa', 'telephone': '1178'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一般情况下，所有的数据做删除操作时，都会有备份的操作\n",
    "person_备份 = person.copy()\n",
    "person_备份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5ec030fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jessa', 'telephone': '1178'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"country\" in person:\n",
    "    person.pop(\"country\")\n",
    "person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a586d930",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jessa', 'country': 'usa', 'telephone': '1178'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person_备份"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0761562",
   "metadata": {},
   "source": [
    "-------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8b20d2b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jessa', 'country': 'usa', 'telephone': '1178'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "person = {}\n",
    "person['name'] = \"jessa\"\n",
    "person['country'] = \"usa\"\n",
    "person['telephone'] = \"1178\"\n",
    "print(person)\n",
    "print(type(person))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "983c9493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('telephone', '1178')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6dfff40e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jessa', 'country': 'usa'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c92ba37",
   "metadata": {},
   "source": [
    "> 5. 关于字典的特殊（高级）用法\n",
    ">> 1. fromkeys()\n",
    ">> 2. setfault()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5c4eb23",
   "metadata": {},
   "source": [
    "---------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "891fc882",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jessa', 'country': 'usa', 'telephone': '1178'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "person = {}\n",
    "person['name'] = \"jessa\"\n",
    "person['country'] = \"usa\"\n",
    "person['telephone'] = \"1178\"\n",
    "print(person)\n",
    "print(type(person))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ed6849ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(person.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2f579a64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 我们可以创建一个字典，字典的value可以自定义（初始化）\n",
    "\n",
    "name_list = [\"marry\",\"jack\",\"rose\",\"nick\"]\n",
    "my_word = {}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "afc11778",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'marry': 10, 'jack': 10, 'rose': 10, 'nick': 10}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_word.fromkeys(name_list,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a057711",
   "metadata": {},
   "source": [
    "-------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a869d6a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'活泼开朗，非常有主见'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict = {\n",
    "    'mary':'活泼开朗，非常有主见',\n",
    "    'jack':'内向，独立'\n",
    "}\n",
    "tinydict.setdefault('mary','心胸狭隘，斤斤计较')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "77bc9abb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mary': '活泼开朗，非常有主见', 'jack': '内向，独立'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f05ef801",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'心胸狭隘，斤斤计较'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict.setdefault('john','心胸狭隘，斤斤计较')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "2d19ba3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mary': '活泼开朗，非常有主见', 'jack': '内向，独立', 'john': '心胸狭隘，斤斤计较'}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "5d8ddcab",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict['mary'] = \"心胸狭隘，斤斤计较\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "1ed03f31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mary': '心胸狭隘，斤斤计较', 'jack': '内向，独立', 'john': '心胸狭隘，斤斤计较'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82a9d15d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
